package com.kakame.netbook.common.init;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;

import org.apache.commons.lang3.StringUtils;

import com.kakame.netbook.common.utils.PropUtil;
import com.kakame.netbook.common.utils.fastdfs.main.TrackerGroup;

/**
 * Global variables
 *
 * @author Happy Fish / YuQing
 * @version Version 1.11
 */
public class FastdfsConfig {

	public static final String CONF_KEY_CONNECT_TIMEOUT = "fastdfs.connect_timeout";
	public static final String CONF_KEY_NETWORK_TIMEOUT = "fastdfs.network_timeout";
	public static final String CONF_KEY_CHARSET = "fastdfs.charset";
	public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http.anti_steal_token";
	public static final String CONF_KEY_HTTP_SECRET_KEY = "fastdfs.http.secret_key";
	public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http.tracker_http_port";
	public static final String CONF_KEY_TRACKER_SERVER = "fastdfs.tracker_server";
	public static final String CONF_FILE_DOWNLOAD_ADDRESS = "fastdfs.download_address";

	public static final int DEFAULT_CONNECT_TIMEOUT = 5; // second
	public static final int DEFAULT_NETWORK_TIMEOUT = 30; // second
	public static final String DEFAULT_CHARSET = "UTF-8";
	public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false;
	public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
	public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;

	public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; // millisecond
	public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; // millisecond
	public static String g_charset = DEFAULT_CHARSET;
	public static String file_download_address = "";
	public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; // if anti-steal token
	public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; // generage token secret key
	public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT;

	public static TrackerGroup g_tracker_group;

	private FastdfsConfig() {
	}

	/**
	 * 加载fastdfs配置文件
	 * @auth 何家明
	 * @date 2018年11月23日 下午2:44:34
	 * @throws IOException
	 * @throws MyException
	 */
	public static void init() {
		String[] szTrackerServers;
		String[] parts;
		
		g_connect_timeout = PropUtil.APP.getInt(CONF_KEY_CONNECT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
		if (g_connect_timeout < 0) {
			g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
		}
		g_connect_timeout *= 1000; // millisecond

		g_network_timeout = PropUtil.APP.getInt(CONF_KEY_NETWORK_TIMEOUT, DEFAULT_NETWORK_TIMEOUT);
		if (g_network_timeout < 0) {
			g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
		}
		g_network_timeout *= 1000; // millisecond

		g_charset = PropUtil.APP.get(CONF_KEY_CHARSET, DEFAULT_CHARSET);

		file_download_address = PropUtil.APP.get(CONF_FILE_DOWNLOAD_ADDRESS);
		if (StringUtils.isBlank(file_download_address)) {
			throw new IllegalArgumentException("配置文件中没有找到" + CONF_FILE_DOWNLOAD_ADDRESS + "配置项");
		}
		
		String szTrackerServerStr = PropUtil.APP.get(CONF_KEY_TRACKER_SERVER);
		if (StringUtils.isBlank(szTrackerServerStr)) {
			throw new IllegalArgumentException("配置文件中没有找到" + CONF_KEY_TRACKER_SERVER + "配置项");
		}
		szTrackerServers = szTrackerServerStr.split(",");

		InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
		for (int i = 0; i < szTrackerServers.length; i++) {
			parts = szTrackerServers[i].split("\\:", 2);
			if (parts.length != 2) {
				throw new IllegalArgumentException("the value of item " + CONF_KEY_TRACKER_SERVER + " is invalid, the correct format is host:port");
			}
			tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
		}
		g_tracker_group = new TrackerGroup(tracker_servers);

		g_tracker_http_port = PropUtil.APP.getInt(CONF_KEY_HTTP_TRACKER_HTTP_PORT, DEFAULT_HTTP_TRACKER_HTTP_PORT);
		g_anti_steal_token = PropUtil.APP.getBoolean(CONF_KEY_HTTP_ANTI_STEAL_TOKEN, DEFAULT_HTTP_ANTI_STEAL_TOKEN);
		if (g_anti_steal_token) {
			g_secret_key = PropUtil.APP.get(CONF_KEY_HTTP_SECRET_KEY, DEFAULT_HTTP_SECRET_KEY);
		}
	}


	/**
	 * construct Socket object
	 *
	 * @param ip_addr
	 *            ip address or hostname
	 * @param port
	 *            port number
	 * @return connected Socket object
	 */
	public static Socket getSocket(String ip_addr, int port) {
		Socket sock = new Socket();
		try {
			sock.setSoTimeout(FastdfsConfig.g_network_timeout);
			sock.connect(new InetSocketAddress(ip_addr, port), FastdfsConfig.g_connect_timeout);
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sock;
	}

	/**
	 * construct Socket object
	 *
	 * @param addr
	 *            InetSocketAddress object, including ip address and port
	 * @return connected Socket object
	 */
	public static Socket getSocket(InetSocketAddress addr) {
		Socket sock = new Socket();
		try {
			sock.setSoTimeout(FastdfsConfig.g_network_timeout);
			sock.connect(addr, FastdfsConfig.g_connect_timeout);
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sock;
	}

	public static int getG_connect_timeout() {
		return g_connect_timeout;
	}

	public static void setG_connect_timeout(int connect_timeout) {
		FastdfsConfig.g_connect_timeout = connect_timeout;
	}

	public static int getG_network_timeout() {
		return g_network_timeout;
	}

	public static void setG_network_timeout(int network_timeout) {
		FastdfsConfig.g_network_timeout = network_timeout;
	}

	public static String getG_charset() {
		return g_charset;
	}

	public static void setG_charset(String charset) {
		FastdfsConfig.g_charset = charset;
	}

	public static int getG_tracker_http_port() {
		return g_tracker_http_port;
	}

	public static void setG_tracker_http_port(int tracker_http_port) {
		FastdfsConfig.g_tracker_http_port = tracker_http_port;
	}

	public static boolean getG_anti_steal_token() {
		return g_anti_steal_token;
	}

	public static boolean isG_anti_steal_token() {
		return g_anti_steal_token;
	}

	public static void setG_anti_steal_token(boolean anti_steal_token) {
		FastdfsConfig.g_anti_steal_token = anti_steal_token;
	}

	public static String getG_secret_key() {
		return g_secret_key;
	}

	public static void setG_secret_key(String secret_key) {
		FastdfsConfig.g_secret_key = secret_key;
	}

	public static TrackerGroup getG_tracker_group() {
		return g_tracker_group;
	}

	public static void setG_tracker_group(TrackerGroup tracker_group) {
		FastdfsConfig.g_tracker_group = tracker_group;
	}

	public static String configInfo() {
		String trackerServers = "";
		if (g_tracker_group != null) {
			InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers;
			for (InetSocketAddress inetSocketAddress : trackerAddresses) {
				if (trackerServers.length() > 0)
					trackerServers += ",";
				trackerServers += inetSocketAddress.toString().substring(1);
			}
		}
		return "{" + "\n  g_connect_timeout(ms) = " + g_connect_timeout + "\n  g_network_timeout(ms) = "
				+ g_network_timeout + "\n  g_charset = " + g_charset + "\n  g_anti_steal_token = " + g_anti_steal_token
				+ "\n  g_secret_key = " + g_secret_key + "\n  g_tracker_http_port = " + g_tracker_http_port
				+ "\n  trackerServers = " + trackerServers + "\n}";
	}

}
